const db = require("../../dbUtil/index");

module.exports = {
    //搜索单首歌曲
    oneSong: (getData, data, sql, res) => {
        db.query(sql, [getData], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作错误！",
                    data: {}
                })
            } else {
                var singerName = [];
                var singerID = [];
                //时长
                var arr1 = results[0].songWord.split("[");
                var arr2 = arr1[arr1.length - 1].split("]");
                var time = arr2[0].substring(1, 5);
                for (var i in results) {
                    singerName.push(results[i].singerName);
                    singerID.push(results[i].singerID);
                }
                results[0].singerName = singerName;
                results[0].singerID = singerID;
                data = results[0];
                res.send({
                    code: 200,
                    msg: "数据库操作成功！",
                    data: { data, time }
                })
            }
        })
    },
    //搜索多首歌曲
    manySongs: (getData, data, sql1, sql2, res) => {
        db.query(sql1, [getData], (err, results) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                return res.send({
                    code: 505,
                    msg: "数据库执行错误！",
                    data: {}
                });
            } else {
                for (var i in results) {
                    function c() {
                        return new Promise((resolve, reject) => {
                            if (sql2.indexOf("like") == -1) {
                                db.query(sql2, [getData, results[i].songName], (err2, results2) => {
                                    if (err2) {
                                        reject(err2);
                                    } else {
                                        resolve(results2);
                                    }
                                })
                            } else {
                                db.query(sql2, [results[i].songName], (err2, results2) => {
                                    if (err2) {
                                        reject(err2);
                                    } else {
                                        resolve(results2);
                                    }
                                })
                            }
                        })
                    }
                    c().then((results2) => {
                        var singerName = [];
                        var singerID = [];
                        for (var j in results2) {
                            singerName.push(results2[j].singerName);
                            singerID.push(results2[j].singerID);
                        }
                        results2[0].singerName = singerName;
                        results2[0].singerID = singerID;
                        //时长
                        var arr1 = results2[0].songWord.split("[");
                        var arr2 = arr1[arr1.length - 1].split("]");
                        var time = arr2[0].substring(1, 5);
                        results2[0].time = time;
                        data.push(results2[0]);
                        if (data.length == results.length) {
                            //获取数据库数据
                            return res.send({
                                code: 200,
                                msg: "数据库执行成功！",
                                data: { data }
                            });
                        }
                    }).catch((err) => {
                        //返回一个json对象：状态码、状态描述、数据
                        console.log(err);
                        return res.send({
                            code: 505,
                            msg: "数据库2执行错误！",
                            data: {}
                        });
                    })
                }
            }
        })
    },
    //根据歌曲id查多首歌曲
    manysongsID: (data, results, sql, res, like) => {
        var songList = results[0];
        for (var i in results) {
            function s() {
                return new Promise((resolve, reject) => {
                    if (results[i].sid == undefined) {
                        db.query(sql, [results[i].songID], (err2, results2) => {
                            if (err2) {
                                reject(err2);
                            } else {
                                resolve(results2);
                            }
                        })
                    } else {
                        db.query(sql, [results[i].sid], (err2, results2) => {
                            if (err2) {
                                reject(err2);
                            } else {
                                resolve(results2);
                            }
                        })
                    }
                })
            }
            s().then((results2) => {
                var singerName = [];
                var singerID = [];
                for (var j in results2) {
                    singerName.push(results2[j].singerName);
                    singerID.push(results2[j].singerID);
                }
                results2[0].singerName = singerName;
                results2[0].singerID = singerID;
                //时长
                var arr1 = results2[0].songWord.split("[");
                var arr2 = arr1[arr1.length - 1].split("]");
                var time = arr2[0].substring(1, 5);
                results2[0].time = time;
                data.push(results2[0]);
                if (data.length == results.length) {
                    //获取数据库数据
                    res.send({
                        code: 200,
                        msg: "数据库执行成功！",
                        data: { data, songList, like }
                    });
                }
            }).catch((err2) => {
                //返回一个json对象：状态码、状态描述、数据
                return res.send({
                    code: 505,
                    msg: "数据库2执行错误！",
                    data: {}
                });
            })
        }
    },
    //全部数据
    all: (sql, res) => {
        db.query(sql, [], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作错误！",
                    data: { err },
                })
            } else {
                res.send({
                    code: 200,
                    msg: "数据库操作成功！",
                    data: { results }
                })
            }
        })
    },
    //删除数据
    delete: (sql, getData, res) => {
        db.query(sql, [getData], (err) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败!",
                    data: { err }
                })
            } else {
                res.send({
                    code: 200,
                    msg: "数据库操作成功！",
                    data: {}
                })
            }
        })
    },
    //编辑用户
    userRedact: (sql1, sql2, req, res) => {
        let { userID, userName, age, phone, account, password, gender } = req.body;
        let headImg = "./newusers/" + req.file.originalname;
        let params = [userName, age, phone, account, password, gender, headImg, Number(userID)];
        console.log(params);
        db.query(sql1, [userName, account], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操1作失败！",
                    data: {},
                })
            } else {
                //判断注册是否重复
                if (results.length > 1) {
                    //用户注册失败
                    return res.send({
                        code: 202,
                        msg: "修改失败，用户名或账号重复",
                        data: {}
                    })
                } else {
                    db.query(sql2, params, (err2, results2) => {
                        if (err2) {
                            res.send({
                                code: 505,
                                msg: "数据库2操作失败！",
                                data: { err2 },
                            })
                        } else {
                            if (results2.affectedRows > 0) {//受影响行数
                                return res.send({
                                    code: 200,
                                    msg: "修改成功",
                                    data: {}
                                });
                            } else {
                                res.send({
                                    code: 201,
                                    msg: "修改失败",
                                    data: {}
                                });
                            }
                        }
                    })
                }
            }
        })
    },
    //删除评论
    deleteComments: (comment_uid, word, time, id, sql, res) => {
        db.query(sql.admin.songCommentDelete, [comment_uid, word, time, id], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败！",
                    data: {},
                })
            } else {
                res.send({
                    code: 200,
                    msg: "数据库操作成功！",
                    data: {},
                })
            }
        })
    }
}